<?php

Yii::import("application.components.evaluators.GraderBase");

/**
 * Kelas konret untuk grader simple
 * Objek SimpleGrader akan menjalankan program hasil kompilasi dan mengujinya dengan test cases
 * Setelah grading, hasil grading dapat diambil berupa array
 */
class DrivenGrader extends GraderBase {

    public function __construct($pCompiler) {
        parent::__construct($pCompiler);
    }

    protected function afterGrade() {
        parent::afterGrade();
    }

    protected function beforeGrade() {
        //Copy evaluation file to grading directory
    }

    protected function doGrade() {
        $moevalboxpath = Yii::app()->params['config']['evaluator']['sandboxpath'];
        $currentDir = getcwd();
        chdir($this->compiler->getGradingDirectory());
        
        $tcNum = $this->requestModel->evaluationset->getConfig("testcases");
        $gradeOutput = $this->compiler->getGradingDirectory() . "/grade.out";
        
        $alloutput = "";
        $score = 0;
        for ($i = 1 ; $i <= $tcNum ; $i++) {
            $currentoutput = "\n========================\nTestcase [$i] test\n";
            $output = "";
            $return = $this->execute("$moevalboxpath -e -t 2 -o $gradeOutput -- " . $this->compiler->getOutputFilePath() . " $i 2>&1", $output);
            
            if ($output != "Time limit exceeded") {
                $currentoutput .= file_get_contents($gradeOutput);
            }
            
            $currentoutput .= "Testcase [$i] runtime : " . $output . "\n========================\n";
            if ($return == 0)
                $score += 1;
            
            $alloutput .= $currentoutput;
        }
        
        //print_r(end(explode("\n" , $this->gradeResult['output'])));
        $this->gradeResult['score'] = (double)$score / $tcNum * 100;
        $this->gradeResult['output'] = $alloutput;
        chdir($currentDir);
    }

}

?>
